/*
 Copyright 2012 by Trypha (RK1)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package jaotwinker;

import java.io.*;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Result;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Grayson
 */
public class Cleaner {
  
    public void createCleanXML(String filename, ExcludedItems ei, MainWindow window) {
        DocumentBuilderFactory docFactory;
        DocumentBuilder docBuilder;
        Document doc;
        
        // Try to clean the stupid bad characters
        window.DisplayMessage("Cleaning " + filename + ".");
        Scanner s = null;
        FileWriter outputStream = null;
        try {
            s = new Scanner(new BufferedReader(new FileReader(filename)));
            filename = filename.replace(".xml", "-CLEAN.xml");
            outputStream = new FileWriter(filename);
            while (s.hasNext()) {
                String rep = s.next().replace("c&#x0;&#x0;&#x0;", "");
                rep += " ";
                outputStream.write(rep);
            }
            window.xmlFile=filename;

        } catch (Exception e) {}
        finally {
            s.close();
            try {
                outputStream.close();
            } catch (IOException e) {}
        }
        
        XyphosParser parser = new XyphosParser();
        window.DisplayMessage("Done. Parsing " + filename + "...");
        parser.parse(filename, ei);

        window.DisplayMessage("Done. " + parser.items.size() + " items.");        
        window.DisplayMessage("Building new XML file...");
        try {
            docFactory = DocumentBuilderFactory.newInstance();
            docBuilder = docFactory.newDocumentBuilder();
            doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("Items");
            doc.appendChild(rootElement);

            for (int i=0; i<parser.items.size(); i++) {
                Item item = (Item)parser.items.get(i);
                Element e = item.getXML(doc);
                rootElement.appendChild(e);
            }

            window.DisplayMessage("Done. Saving file...");
            File f = new File("clean.xml");

            // Use a Transformer for output
            Transformer xformer = TransformerFactory.newInstance().newTransformer();

            DOMSource source = new DOMSource(doc);
            Result result = new StreamResult(f);
            xformer.transform(source, result);
            window.DisplayMessage("Done.");
        }
        catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerConfigurationException tce) {
        } catch (TransformerException te) {}
    }
}
